<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "100%",
    },
    height: {
      type: String,
      default: "350px",
    },
    autoResize: {
      type: Boolean,
      default: true,
    },
    chartData: {
      type: Object,
      required: true,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val);
      },
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.setOptions(this.chartData);
    },
    setOptions({ xData, yData, yKey } = {}) {
      let total = [];
      let lenKey = [];
      let seriesData = [];
      let MaxVal = 10;

      for (let key in yData) {
        let items = yData[key];
        console.log(yKey[key]);
        lenKey.push(yKey[key]);
        total = total.concat(items);
        seriesData.push({
          name: yKey[key],

          smooth: true,
          type: "line",
          data: items,
          animationDuration: 2800,
          animationEasing: "cubicInOut",
        });
      }

      console.log(lenKey);

      if (total.length > 0) {
        MaxVal = total.reduce((num1, num2) => {
          return num1 > num2 ? num1 : num2;
        });
      }

      let endPercent = (10 / xData.length) * 100;

      this.chart.setOption({
        xAxis: {
          data: xData,
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        grid: {
          left: 10,
          right: 40,
          bottom: 20,
          top: 30,
          containLabel: true,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
          },
          padding: [5, 10],
        },
        yAxis: {
          axisTick: {
            show: false,
          },
          min: 0,
          max:
            MaxVal < 10
              ? 10
              : MaxVal < 100
              ? Math.ceil(MaxVal / 10) * 10
              : undefined,
          splitNumber: 10,
        },

        dataZoom: {
          // 开启数据平移
          type: "slider", // 滑动条单独显示
          show: xData.length > 10 ? true : false, // 是否显示滑动条
          start: 0, //默认为0
          end: endPercent,
          height: 10, // 滑动条组件高度
          bottom: 0, // 距离图表区域下边的距离
          fillerColor: "#D4D4D4", // 平移条的填充颜色
          borderColor: "transparent", // 边框颜色
          zoomLock: true, // 锁定视图
          brushSelect: false, // 不可缩放 滑动条默认是有手柄可以进行展示的内容区域缩放的，不太美观
          // 通过该属性可以只滑动，不显示缩放功能
          handleStyle: {
            opacity: 0,
          },
          lineStyle: {
            opacity: 0,
          },
          textStyle: {
            fontSize: 0,
          },
        },

        legend: {
          data: lenKey,
        },
        series: seriesData,
      });
    },
  },
};
</script>
